package day7;

import java.util.*;

//测试TreeSet
public class TestTreeSet {
    public static void main(String[] args) {
        //1.数字的TreeSet  默认按照升序 去重
        TreeSet<Integer> set1 = new TreeSet<>(Arrays.asList(3, 2, 5, 1, 7, 6, 9, 6, 9));
        //System.out.println(set1);


        /*Integer[] nums = {1,3,5,7,9};
        List<Integer> list = Arrays.asList(nums);
        //list.add(1,45);       //缺点:asList生成的集合是不可变集合，只能看不能改*/


        //2.String类 按位比较
        TreeSet<String> set2 = new TreeSet<>(Arrays.asList("cb", "ba", "ac"));
        Set<String> set3 = new TreeSet<>(((o1, o2) -> o2.compareTo(o1)));
        set3.addAll(set2);
        //System.out.println(set3);

        //3.对象类排序:  斗地主 针对于牌对象排序
        //♠ 10  ♥J  ♦10    🃏    🃏
        Set<Card> set4 = new TreeSet<>((c1, c2) -> {    //比较器
            int i = c2.num - c1.num;//根据数值排序
            if (i == 0) i = c1.str.compareTo(c2.str);
            //如果数值相同的话，再按照花色排序
            return i;
        });
        //定义所有牌对象   定义存储list所有排对象
        String[] color = {"♣", "♦", "♠", "♥"};
        String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "DemoA", "2"};
        List<Card> cards = new ArrayList<>();
        for (String s : color) {
            for (int i = 0; i < num.length; i++) {
                cards.add(new Card(s + num[i], i + 3));
            }
        }
        cards.add(new Card("小🃏", 16));
        cards.add(new Card("大🃏", 17));
        System.out.println(cards);

        Collections.shuffle(cards); //打乱顺序
        Random r = new Random();
        for (int i = 0; i < 17; i++) {
            int index = r.nextInt(cards.size());
            set4.add(cards.get(index));
        }
        System.out.println(set4);
    }
}

class Card {
    String str; //存储内容  ♣10
    int num;    //存储排对应的数值

    public Card(String str, int num) {
        this.str = str;
        this.num = num;
    }

    @Override
    public String toString() {
        return str;
    }
}